//
//  ZJSCargoValueSlider.m
//  ZallDemo
//
//  Created by so on 16/9/5.
//  Copyright © 2016年 so. All rights reserved.
//

#import "ZJSCargoValueSlider.h"

static CGFloat const ZJSCargoValueBlockWidth = 28.0f;
static CGFloat const ZJSCargoBlockInsets = -10.0f;

@interface ZJSCargoSliderBoderView : UIImageView
@property (strong, nonatomic, readonly) UILabel *textLabel;
@end

@implementation ZJSCargoSliderBoderView
@synthesize textLabel = _textLabel;

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.textLabel];
    }
    return (self);
}

- (void)sizeToFit {
    [self.textLabel sizeToFit];
    [super sizeToFit];
    self.bounds = UIEdgeInsetsInsetRect(self.textLabel.bounds, UIEdgeInsetsMake(-5, -8, -8, -8));
}

- (UILabel *)textLabel {
    if(!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.textColor = [UIColor blackColor];
        _textLabel.font = [UIFont systemFontOfSize:14.0f];
        _textLabel.textAlignment = NSTextAlignmentCenter;
    }
    return (_textLabel);
}

@end


@interface ZJSCargoValueSlider () <UIGestureRecognizerDelegate> {
    CGFloat _offset;
}

@property (strong, nonatomic, readonly) UIView *blockView;

@property (strong, nonatomic, readonly) ZJSCargoSliderBoderView *progressTextView;

@end


@implementation ZJSCargoValueSlider
@synthesize blockView = _blockView;
@synthesize progressTextView = _progressTextView;

#pragma mark - life cycle
- (void)dealloc {
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.clipsToBounds = NO;
        self.backgroundColor = [UIColor clearColor];
        _progressWidth = 8.0f;
        _value = 0.0f;
        _maximumValue = 1.0f;
        _minimumValue = 0;
        CGFloat sub = (_maximumValue - _minimumValue);
        _offset = ((sub == 0) ? 0.5f : _value / sub);
        self.minimumValueColor = [UIColor lightGrayColor];
        self.maximumValueColor = UIColorFromHexRGB(0xff6600);
        [self addSubview:self.blockView];
        [self addSubview:self.progressTextView];
        self.needShowText = NO;
        
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
        panGesture.delegate = self;
        [self addGestureRecognizer:panGesture];
    }
    return (self);
}
#pragma mark -

#pragma mark - override
- (void)layoutSubviews {
    [super layoutSubviews];
    self.blockView.center = CGPointMake(ZJSCargoValueBlockWidth / 2.0f + (CGRectGetWidth(self.bounds) - ZJSCargoValueBlockWidth) * _offset, CGRectGetHeight(self.bounds) / 2.0f);
    self.progressTextView.center = CGPointMake(2.0f + CGRectGetMinX(self.blockView.frame) + CGRectGetWidth(self.progressTextView.bounds) / 2.0f, CGRectGetMinY(self.blockView.frame) - CGRectGetHeight(self.progressTextView.bounds) / 2.0f);
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGSize size = self.bounds.size;
    if(size.width <= 0) {
        return;
    }
    
    CGFloat drawOffset = _offset;
    drawOffset = MAX(drawOffset, (self.progressWidth / 2.0f) / size.width);
    drawOffset = MIN(drawOffset, 1.0f - (self.progressWidth / 2.0f) / size.width);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(ctx);
    
    CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(ctx, self.progressWidth);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    
    CGContextSetFillColorWithColor(ctx, self.maximumValueColor.CGColor);
    CGContextAddEllipseInRect(ctx, CGRectMake(0, (size.height - self.progressWidth) / 2.0f, self.progressWidth, self.progressWidth));
    CGContextFillPath(ctx);
    
    CGContextSetStrokeColorWithColor(ctx, self.maximumValueColor.CGColor);
    CGContextMoveToPoint(ctx, self.progressWidth / 2.0f, size.height / 2.0f);
    CGContextAddLineToPoint(ctx, size.width * drawOffset, size.height / 2.0f);
    CGContextStrokePath(ctx);
    
    CGContextSetFillColorWithColor(ctx, self.minimumValueColor.CGColor);
    CGContextAddEllipseInRect(ctx, CGRectMake(size.width - self.progressWidth, (size.height - self.progressWidth) / 2.0f, self.progressWidth, self.progressWidth));
    CGContextFillPath(ctx);
    
    CGContextSetStrokeColorWithColor(ctx, self.minimumValueColor.CGColor);
    CGContextMoveToPoint(ctx, size.width * drawOffset, size.height / 2.0f);
    CGContextAddLineToPoint(ctx, size.width - self.progressWidth / 2.0f, size.height / 2.0f);
    CGContextStrokePath(ctx);
    
    UIGraphicsPopContext();
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return (CGRectContainsPoint(UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(ZJSCargoBlockInsets, ZJSCargoBlockInsets, ZJSCargoBlockInsets, ZJSCargoBlockInsets)), point));
}
#pragma mark -

#pragma mark - setter
- (void)setProgressWidth:(CGFloat)progressWidth {
    [self willChangeValueForKey:@"progressWidth"];
    _progressWidth = MAX(1.0f, progressWidth);
    [self didChangeValueForKey:@"progressWidth"];
    [self setNeedsDisplay];
    [self setNeedsLayout];
}

- (void)setValue:(CGFloat)value {
    [self willChangeValueForKey:@"value"];
    _value = MIN(_maximumValue, MAX(_minimumValue, value));
    [self didChangeValueForKey:@"value"];
    CGFloat sub = (_maximumValue - _minimumValue);
    [self willChangeValueForKey:@"offset"];
    _offset = ((sub == 0) ? 0.5f : _value / sub);
    [self didChangeValueForKey:@"offset"];
    [self setNeedsDisplay];
    [self setNeedsLayout];
}

- (void)setMinimumValue:(CGFloat)minimumValue {
    [self willChangeValueForKey:@"minimumValue"];
    _minimumValue = minimumValue;
    [self didChangeValueForKey:@"minimumValue"];
    CGFloat sub = (_maximumValue - _minimumValue);
    [self willChangeValueForKey:@"offset"];
    _offset = ((sub == 0) ? 0.5f : _value / sub);
    [self didChangeValueForKey:@"offset"];
    [self setNeedsDisplay];
}

- (void)setMaximumValue:(CGFloat)maximumValue {
    [self willChangeValueForKey:@"maximumValue"];
    _maximumValue = maximumValue;
    [self didChangeValueForKey:@"maximumValue"];
    CGFloat sub = (_maximumValue - _minimumValue);
    [self willChangeValueForKey:@"offset"];
    _offset = ((sub == 0) ? 0.5f : _value / sub);
    [self didChangeValueForKey:@"offset"];
    [self setNeedsDisplay];
}

- (void)setMinimumValueColor:(UIColor *)minimumValueColor {
    [self willChangeValueForKey:@"minimumValueColor"];
    _minimumValueColor = minimumValueColor;
    [self didChangeValueForKey:@"minimumValueColor"];
    [self setNeedsDisplay];
}

- (void)setMaximumValueColor:(UIColor *)maximumValueColor {
    [self willChangeValueForKey:@"maximumValueColor"];
    _maximumValueColor = maximumValueColor;
    [self didChangeValueForKey:@"maximumValueColor"];
    self.progressTextView.textLabel.textColor = _maximumValueColor;
    [self setNeedsDisplay];
}

- (void)setNeedShowText:(BOOL)needShowText {
    [self willChangeValueForKey:@"needShowText"];
    _needShowText = needShowText;
    [self didChangeValueForKey:@"needShowText"];
    self.progressTextView.hidden = !_needShowText;
    [self setNeedsLayout];
}

- (void)setText:(NSString *)text {
    [self willChangeValueForKey:@"text"];
    self.progressTextView.textLabel.text = text;
    [self didChangeValueForKey:@"text"];
    [self.progressTextView sizeToFit];
    [self setNeedsLayout];
}
#pragma mark -

#pragma mark - getter
- (NSString *)text {
    return (self.progressTextView.textLabel.text);
}

- (UIView *)blockView {
    if(!_blockView) {
        _blockView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ZJSCargoValueBlockWidth, ZJSCargoValueBlockWidth)];
        _blockView.backgroundColor = [UIColor whiteColor];
        _blockView.layer.cornerRadius = ZJSCargoValueBlockWidth / 2.0f;
        _blockView.layer.borderWidth = 1.0f;
        _blockView.layer.borderColor = self.maximumValueColor.CGColor;
    }
    return (_blockView);
}

- (ZJSCargoSliderBoderView *)progressTextView {
    if(!_progressTextView) {
        _progressTextView = [[ZJSCargoSliderBoderView alloc] initWithFrame:CGRectZero];
        _progressTextView.backgroundColor = [UIColor clearColor];
        _progressTextView.image = [[UIImage imageNamed:@"icon_box"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f, 17.0f, 10.0f, 5.0f)];
        _progressTextView.textLabel.textColor = self.maximumValueColor;
        _progressTextView.textLabel.font = [UIFont systemFontOfSize:14.0f];
        _progressTextView.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    return (_progressTextView);
}
#pragma mark -

#pragma mark - Action
- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer {
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:{
//            self.blockView.backgroundColor = self.maximumValueColor;
//            self.blockView.layer.borderColor = [UIColor whiteColor].CGColor;
            CGPoint point = [gestureRecognizer locationInView:self];
            self.value = ((point.x - ZJSCargoValueBlockWidth / 2.0f) / (CGRectGetWidth(self.bounds) - ZJSCargoValueBlockWidth)) * (self.maximumValue - self.minimumValue);
            [self sendActionsForControlEvents:UIControlEventTouchDown];
        }break;
        case UIGestureRecognizerStateChanged:{
            CGPoint point = [gestureRecognizer locationInView:self];
            self.value = ((point.x - ZJSCargoValueBlockWidth / 2.0f) / (CGRectGetWidth(self.bounds) - ZJSCargoValueBlockWidth)) * (self.maximumValue - self.minimumValue);
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:{
//            self.blockView.backgroundColor = [UIColor whiteColor];
//            self.blockView.layer.borderColor = self.maximumValueColor.CGColor;
            [self sendActionsForControlEvents:UIControlEventTouchCancel];
        }break;
        default:break;
    }
}
#pragma mark -

#pragma mark - <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    CGPoint point = [gestureRecognizer locationInView:self];
    return (CGRectContainsPoint(UIEdgeInsetsInsetRect(self.blockView.frame, UIEdgeInsetsMake(ZJSCargoBlockInsets, ZJSCargoBlockInsets, ZJSCargoBlockInsets, ZJSCargoBlockInsets)), point));
}
#pragma mark -

@end
